// 路由文件
const express=require("express");
const userLogin = require("../service/userLogin");
const authorsList = require("../service/authorsList");
const classifyArticleList = require("../service/classifyArticleList");
const articleList = require("../service/articleList");
const myArticleList= require("../service/myArticleList");
const deleteArticle= require("../service/deleteArticle");
const router=express.Router();
router.post("/userLogin",(req,res)=>{
    let params=req.body
    let username=params.userName
    let userpwd=params.userPassword
    console.log(username,userpwd)
    // userLogin(username,userpwd)
    // res.send("登录成功")

userLogin(username,userpwd).then(data=>{
        if(data==501){
            res.json({
                status:data,
                msg:"用户不存在"
            })
        }else if(data==502){
            res.json({
                status:data,
                msg:"错误"
            })
        }else{
            res.json({
                status:200,
                msg:{_id:Buffer.from(String(data.userId)).toString("base64"),
                    userName:data.userName,
                    headImg:data.headImg
            }
            })
            res.end()
        }
    })
})
router.get("/authorsList",(req,res)=>{
    authorsList().then(data=>{
        if(data==404){
            res.json({
                status:data,
                msg:"作者列表为空"
            })
            res.end()
        }else{
            res.json({
                status:200,
                msg:data
            })
            res.end()
        }
    })
    // res.send("ok")
})
router.get("/classifyArticleList",(req,res)=>{
    let url1 ="http://"+req.headers.host+req.url;
    let url2 = new URL(url1)
    let params =url2.searchParams;
    let classify = params.get("classify")
    classifyArticleList(classify).then(data=>{
        if(data == 501){
            res.json({
                status : data,
                msg : "classify不存在"
            })
        }else{
            res.json({
                status : 200,
                msg : data
            })
        }
    })
})

router.get("/articleList",(req,res)=>{
    articleList().then(data=>{
        if(data==404){
            res.json({
                status:data,
                msg:"文章内容为空"
            })
            res.end()
        }else{
            res.json({
                status:200,
                msg:data
            })
            res.end()
        }
    })
    // res.send("ok")
})

router.get("/myArticleList",(req,res)=>{
    myArticleList().then(data=>{
        if(data==404){
            res.json({
                status:data,
                msg:"文章内容为空"
            })
            res.end()
        }else{
            res.json({
                status:200,
                msg:data
            })
            res.end()
        }
    })
    // res.send("ok")
})

router.get("/deleteArticle",(req,res)=>{
    let url3 ="http://"+req.headers.host+req.url;
    let url4= new URL(url3)
    let params =url4.searchParams;
    let  id= params.get("id")
    deleteArticle(id).then(data=>{
        if(data == 501){
            res.json({
                status : data,
                msg : "删除失败"
            })
        }else{
            res.json({
                status : 200,
                msg : data
            })
        }
    })
})
module.exports=router;
// create table user(
//     userId varchar(100)  not null,
//     userName varchar(100)  not null,
//     userPassword varchar(255)  not null,
//     headImg varchar(200)  not null);
//     insert into user(userId,userName,userPassword,headImg)values
//     ("123","admin","888","headimg7.png"),("456","alice","789","headimg8.png");
// create table author(
//     _id varchar(100)  not null,
//     userName varchar(100)  not null,
//     headImg varchar(255)  not null,
//     descs varchar(200)  not null);

// insert into author(_id,userName,headImg,descs)values
//    ("6123","lilei","headimg1.png","lalala"),("456","admin","headimg7.png","super me");